package com.lyfy.sd.order.dao.mapper;

import com.lyfy.sd.order.dao.entity.OrderItem;
import com.lyfy.sd.order.dao.entity.OrderItemDetail;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;
import java.util.Set;

public interface OrderItemDetailMapper extends Mapper<OrderItem> {


    @Select("SELECT\n" +
            "om.order_master_id AS orderMasterId,\n" +
            "om.user_id AS userId,\n" +
            "om.order_status AS orderStatus,\n" +
            "om.pay_status AS payStatus,\n" +
            "oi.order_item_id AS orderItemId,\n" +
            "oi.pay_amt AS payAmt,\n" +
            "oi.point_amt AS pointAmt,\n" +
            "oi.create_d AS createD,\n" +
            "oi.creator AS creator,\n" +
            "oi.update_d AS updateD,\n" +
            "oi.updator AS updator,\n" +
            "oi.modify_d AS modifyD \n" +
            "FROM\n" +
            "order_item oi\n" +
            "LEFT JOIN order_master om ON oi.order_master_id = om.order_master_id \n" +
            "WHERE\n" +
            "oi.order_master_id = #{orderMasterId}")
    List<OrderItemDetail> getOrderItemDetailByOrderMasterId(@Param("orderMasterId") Long orderMasterId);



    @Select("SELECT\n" +
            "om.order_master_id AS orderMasterId,\n" +
            "om.user_id AS userId,\n" +
            "om.order_status AS orderStatus,\n" +
            "om.pay_status AS payStatus,\n" +
            "oi.order_item_id AS orderItemId,\n" +
            "oi.pay_amt AS payAmt,\n" +
            "oi.point_amt AS pointAmt,\n" +
            "oi.create_d AS createD,\n" +
            "oi.creator AS creator,\n" +
            "oi.update_d AS updateD,\n" +
            "oi.updator AS updator,\n" +
            "oi.modify_d AS modifyD \n" +
            "FROM\n" +
            "order_item oi\n" +
            "LEFT JOIN order_master om ON oi.order_master_id = om.order_master_id \n" +
            "WHERE\n" +
            "oi.user_id = #{userId} and om.user_id is not null")
    List<OrderItemDetail> getOrderItemDetailByUserId(@Param("userId") Long userId);


    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    class OrderMasterPayAmt{
        private Long userId;
        private Long orderMasterId;
        private Long payAmt;
        private Long pointAmt;
    }

    @Select("SELECT\n" +
            "oi.user_id AS userId,\n" +
            "SUM( oi.pay_amt ) AS payAmt,\n" +
            "SUM( oi.point_amt ) AS pointAmt \n" +
            "FROM\n" +
            "order_item oi\n" +
            "LEFT JOIN order_master om on om.order_master_id = oi.order_master_id \n" +
            "WHERE\n" +
            "oi.user_id = #{userId}\n" +
            "AND om.pay_status = 1 \n" +
            "GROUP BY\n" +
            "oi.user_id HAVING payAmt >=100")
    List<OrderMasterPayAmt> getOrderMasterPayAmtByUser(@Param("userId") Long userId);


    @Select("<script>" +
            "SELECT\n" +
            "oi.order_master_id AS orderMasterId,\n" +
            "SUM( oi.pay_amt ) AS payAmt,\n" +
            "SUM( oi.point_amt ) AS pointAmt \n" +
            "FROM\n" +
            "order_item oi\n" +
            "LEFT JOIN order_master om on om.order_master_id = oi.order_master_id \n" +
            "WHERE\n" +
            "oi.order_master_id IN"
            + " <foreach collection='orderMasterIds' item='orderMasterId' open='(' separator=',' close=')'> "
            + "    #{orderMasterId} "
            + " </foreach> "+
            "AND om.pay_status = 1 \n" +
            "GROUP BY\n" +
            "oi.order_master_id HAVING payAmt >=100"+
            "</script>")
    List<OrderMasterPayAmt> getOrderMasterPayAmtById(@Param("orderMasterIds") Set<Long> orderMasterIds);
}